setwd("~/Documents/replication_packages/thesis/")
db <- 'pp_forum_apr2014.sqlite'
db_anonym <- 'pp_forum_apr2014_anonymised.sqlite'

sqliteGetTable <- function(database, table) {
  require(DBI)
  require(RSQLite)
  con <- dbConnect(RSQLite::SQLite(), dbname = database)
  query <- dbSendQuery(con, paste("SELECT * FROM ", table, ";", sep="")) 
  result <- fetch(query, n = -1)
  dbClearResult(query)
  dbDisconnect(con)
  return(result)
}

sqliteListTables <- function(database, table) {
  require(DBI)
  require(RSQLite)
  con <- dbConnect(RSQLite::SQLite(), dbname = database)
  return(dbListTables(con))
}

sqliteWriteTable <- function(database, table_name, df) {
  require(DBI)
  require(RSQLite)
  con <- dbConnect(RSQLite::SQLite(), dbname = database)
  dbWriteTable(con, table_name, df)
}

tables <- sqliteListTables(db)

author_tbl <- sqliteGetTable(db, 'author')
diff_tbl <- sqliteGetTable(db, 'diff')
map_tbl <- sqliteGetTable(db, 'map')
post_tbl <- sqliteGetTable(db, 'post')
thread_tbl <- sqliteGetTable(db, 'thread')

library(openssl)
author_tbl$authorName <- md5(author_tbl$authorName)

sqliteWriteTable(db_anonym, 'author', author_tbl)
sqliteWriteTable(db_anonym, 'diff', diff_tbl)
sqliteWriteTable(db_anonym, 'map', map_tbl)
sqliteWriteTable(db_anonym, 'post', post_tbl)
sqliteWriteTable(db_anonym, 'thread', thread_tbl)
